﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;

namespace DuplexServiceClient
{
    public partial class Form1 : Form
    {
        InstanceContext context = null;
        ServiceReference1.ServiceClient client = null;
        static string myNickname = string.Empty;
        public Form1()
        {
            InitializeComponent();
            StaticDepender.listBox = listBox1;
            StaticDepender.contactsBox = contactsListBox;
            context = new InstanceContext(new CallbackImplementation());
            client = new ServiceReference1.ServiceClient(context);
   //         client.ClientCredentials.Windows.ClientCredential.UserName = "windowsuseraccountname";
   //         client.ClientCredentials.Windows.ClientCredential.Password = "windowsuseraccountpassword";
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            client.SendMessageToService(textBox1.Text, (string)contactsListBox.SelectedItem);
            StaticDepender.listBox.Items.Add(string.Format("({0}) Вы : {1}", DateTime.Now.ToShortTimeString(), textBox1.Text));
            button1.Enabled = false;
            textBox1.Text = string.Empty;
        }

        private void nicknameBox_TextChanged(object sender, EventArgs e)
        {
            enterChatButton.Enabled = nicknameBox.Text.Length > 0;
        }

        private void enterChatButton_Click(object sender, EventArgs e)
        {
            myNickname = nicknameBox.Text;
            client.AddNewClient(myNickname);
            (sender as Button).Enabled = false;
            nicknameBox.Enabled = false;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            client.DisconnectFromServer(myNickname);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            button1.Enabled = textBox1.Text.Length > 0;
        }
    }
}
